﻿package framework.core;

import java.util.*;
import java.sql.*;
import java.text.*;
import java.io.*;

/**
 * Creation date: (2003-08-28)
 * @author: adminnistrator
 * PreparedStatement包装类
 */
public class DBPreparedStatement 
{
    private final static org.apache.log4j.Logger log =
        org.apache.log4j.Logger.getLogger("LOGGER");   // 日志类
    
	private java.sql.PreparedStatement m_pst = null;  //java.sql.PreparedStatement
	private String m_sql = null;                      //sql statement
	private Vector m_params = new Vector();           //sql parameters
	 
	private static int m_instance_count = 0 ;
	private static int getInstanceCount()
	{
		m_instance_count ++ ;
		return m_instance_count;
	}
	private int m_id = 0 ;
	public int getId()
	{
		return m_id ;
	} 
	public DBPreparedStatement(java.sql.PreparedStatement pst,String sql)
	{
		m_pst = pst;
		m_sql = sql;
		m_id = getInstanceCount();
		//Logger.log(this,"opened");
	}
	public PreparedStatement getPst()
	{
		return m_pst ;
	}
	
	public void close() throws Exception
	{
		if(m_pst!=null)
		{
			m_pst.close();
			//Logger.log(this,"closed");
		}
	}
	public ResultSet executeQuery() throws Exception
	{
		try
		{
			if(m_pst!=null)
			{
				java.sql.ResultSet rs = m_pst.executeQuery();
				framework.core.Logger.log(this,"executeQueryed,"+viewSql());
                
				return rs;
			}
			return null;
		}
		catch(Exception e)
		{
            framework.core.Logger.log(this,"SQL Query Exception,"+viewSql());
            log.error(viewSql(), e);
			throw e ;
		}
	}
	public int executeUpdate() throws Exception
	{
		try
		{
			if(m_pst!=null)
			{
				//Logger.log(this,"before executeUpdate,"+viewSql());
				int count = m_pst.executeUpdate();
                framework.core.Logger.log(this,"executeUpdated,"+viewSql());
				return count;
			}
			return -1;
		}
		catch(Exception e)
		{
            framework.core.Logger.log(this,"SQL Update Exception,"+viewSql());
            log.error(viewSql(), e);
			throw e ;
		}
	}
	
	public void setParam(int index,Value x) throws Exception
	{
		if(m_pst!=null)
		{
			if(x.isInt())
			{
				if(x.getInt()==Value.NULL_INT)
				{
					m_pst.setNull(index,java.sql.Types.NUMERIC);
				}
				else
				{
					m_pst.setInt(index,x.getInt());
				}
			}
			else if(x.isDouble())
			{
				if(x.getDouble()==Value.NULL_DOUBLE)
				{
					m_pst.setNull(index,java.sql.Types.NUMERIC);
				}
				else
				{
					m_pst.setDouble(index,x.getDouble());
				}
			}
			else if(x.isString())
			{
				String v = x.isNull() ? null : x.getString();
				m_pst.setString(index,v);
			}
			else if(x.isDateUtil())
			{
				String v = x.isNull() ? null : x.getDateUtil().toString() ;
				m_pst.setString(index,v);
			}
			else if(x.isBytes())
			{
				byte[] v = x.getBytes();
				InputStream stream = new java.io.ByteArrayInputStream(v);
				m_pst.setBinaryStream(index,stream,stream.available());
			}
			else
			{
                framework.core.Logger.throwException(this,"setParam("+x+")未知的数据类型!");
			}
			m_params.insertElementAt(x,index-1);
		}
	}

	public void setInt(int parameterIndex,int x) throws Exception
	{
		setParam(parameterIndex,new Value(x));
	}
	public void setDouble(int parameterIndex,double x) throws Exception
	{
		setParam(parameterIndex,new Value(x));
	}
	public void setString(int parameterIndex,String x) throws Exception
	{
		setParam(parameterIndex,new Value(x));
	}
	public void setDateUtil(int parameterIndex,DateUtil x) throws Exception
	{
		setParam(parameterIndex,new Value(x));
	}
	public void setBytes(int parameterIndex,byte[] x) throws Exception
	{
		setParam(parameterIndex,new Value(x));
	}
		
	public String toString()
	{
		DecimalFormat fmt = new DecimalFormat("########");
		return "DBPreparedStatement-"+fmt.format(getId())+"" ;
	}
	
	public String viewSql()
	{
		String sql = "sql="+m_sql ;
		
		try {
			for(int i=0;i<m_params.size();i++)
			{
				Value value = (Value)m_params.get(i);
	//			if(i==0)
	//			{
	//				sql += ":" + value;
	//			}
	//			else
	//			{
	//				sql += "," + value;
	//			}
				if(value.getType() == Value.TYPE_STRING)
					sql = StringUtil.replaceFirst(sql, "?", "'"+value.getString() + "'");
				else if(value.getType() == Value.TYPE_INT)
					sql = StringUtil.replaceFirst(sql, "?", String.valueOf(value.getInt()));
				else if(value.getType() == Value.TYPE_DOUBLE)
					sql = StringUtil.replaceFirst(sql, "?", String.valueOf(value.getDouble()));
				else ;	
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return sql;
	}
}

